package com.submit.demo.controller;

import com.submit.demo.common.api.CommonResult;
import com.submit.demo.model.ColTag;
import com.submit.demo.repository.ColTagRepository;
import com.submit.demo.service.ColSubService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("tag")
public class TagController {
    @Autowired
    private ColTagRepository colTagRepository;

    @Autowired
    private ColSubService colSubService;

    @ApiOperation("获取标签列表")
    @ResponseBody
    @RequestMapping(value = "/list")
    public CommonResult<List<String>> getList(@RequestParam("cid") String cid){
        return CommonResult.success(colTagRepository.findColTagByCid(cid).getTags());
    }

    @ApiOperation("新建标签")
    @ResponseBody
    @RequestMapping(value = "/crTag")
    public CommonResult crTag(@RequestParam("cid") String cid, @RequestParam("tag") String tag){
        ColTag colTag = colTagRepository.findColTagByCid(cid);
        if(colTag.getTags().contains(tag)) return CommonResult.failed("该标签已存在");
        else{
            colTag.getTags().add(tag);
            colTagRepository.save(colTag);
            return CommonResult.success(null, "创建成功");
        }
    }

    @ApiOperation("为一个标签指定多个提交人")
    @ResponseBody
    @RequestMapping(value = "/crTagWithSubs")
    public CommonResult crTagWithSubs(@RequestParam("cid") String cid, @RequestParam("name") String tag, @RequestParam("ids") List<String> ids){
        Integer result = colSubService.insertTagWithSubs(cid, tag, ids);
        if(result == 1) return CommonResult.success(null, "操作成功");
        else return CommonResult.failed("失败");
    }
}
